<?php

namespace App\Http\Controllers\Dashboard;


use App\Models\Org;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;


class OrgController extends BaseController
{
    /**
     * 获取当前登录机构的机构树
     * @param Request $request
     * @return JsonResponse
     */
    public function tree(Request $request) {
        //只能展示自己有权限的菜单树
        $user = $request->user();
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'tree' => getOrgTree($user)
            ]
        ]);
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function edit(Request $request, $id) {
        $org = Org::ofOrgCode($request->user())->find($id);
        if (empty($org)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        }

        $org->name = (string) $request->name;
        $org->addr = (string) $request->addr;

        if ($org->save()) {
            return response()->json([
                'code' => 0,
                'msg' => '保存成功',
                'data' => $org
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '保存失败',
                'data' => null
            ]);
        }
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function create(Request $request) {
        $orgParent = Org::ofOrgCode($request->user())->find($request->pid);
        if (empty($orgParent)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        }

        $org = new Org();
        $org->pid = $request->pid;
        $org->name = (string)$request->name;
        $org->addr = (string) $request->addr;
        $org->org_root = $orgParent->org_root;
        $org->org_code = createSubOrgcode($orgParent->org_code);

        if ($org->save()) {
            return response()->json([
                'code' => 0,
                'msg' => '保存成功',
                'data' => $org
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '保存失败',
                'data' => null
            ]);
        }
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detail(Request $request, $id)
    {
        $org = Org::ofOrgCode($request->user())->find($id);
        if (empty($org)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => 'ok',
                'data' => $org
            ]);
        }
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function manager(Request $request, $id)
    {
        $org = Org::ofOrgCode($request->user())->find($id);
        if (empty($org)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => 'ok',
                'data' => User::where('org_code', $org->org_code)->where('is_manager', 1)->get()
            ]);
        }
    }

    /**
     * 设置管理员
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function setManager(Request $request, $id)
    {
        $org = Org::ofOrgCode($request->user())->find($id);
        if (empty($org)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        }

        $model = User::where('org_code', $org->org_code)->find($request->user_id);
        if(empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '当前机构下不存在该用户',
                'data' => null
            ]);
        }

        $model->is_manager = (bool) $request->is_manager;

        if ($model->save()) {
            return response()->json([
                'code' => 0,
                'msg' => '操作成功',
                'data' => $model
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '操作失败',
                'data' => null
            ]);
        }
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function user(Request $request, $id)
    {
        $org = Org::ofOrgCode($request->user())->find($id);
        if (empty($org)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => 'ok',
                'data' =>  User::where('org_code', $org->org_code)->where(function ($query) use($request) {
                    $query->where('name', "like", sprintf("%%%s%%",  $request->k))
                        ->Orwhere('mobile', "like", sprintf("%%%s%%",  $request->k));
                })->limit(10)->select(["id","name","mobile"])->get()->makeHidden( ['status_name', 'gender_name'])
            ]);
        }
    }
}
